import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: () => import('@/components/login.vue')
    },
    {
        path: '/home',
        component: () => import('@/components/home.vue'),
        redirect: '/welcome',
        children: [
            {
                path: '/welcome', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/welcome.vue'),
            },
            {
                path: '/users', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/users.vue'),
            },
            {
                path: '/roles', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/roles.vue'),
            },
            {
                path: '/rights', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/rights.vue'),
            },
            {
                path: '/params', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/params.vue'),
            },
            {
                path: '/categories', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/categories.vue'),
            },
            {
                path: '/goods', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/goods.vue'),
            },
            {
                path: '/orders', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/orders.vue'),
            },
            {
                path: '/reports', // 如果是重定向到父路由中那么就要加/
                component: () => import('@/components/reports.vue'),
            },

        ]
    }
]

const router = new VueRouter({
    routes
})


router.beforeEach((to, from, next) => {
    // 如果去的不是login 并且没有token就跳回登录页面
    if (to.path !== '/login' && !window.sessionStorage.getItem('token')) {
        return next('/login')
    }
    // console.log(from);
    // console.log(next);
    next()
})

export default router